
#include "tMere.h"

void tReceptOrdre()
{
	return;
}

void tEnvoi()
{
	Trame * trame;
	for( ; ; )
	{
		trame = (Trame *) malloc(sizeof(Trame));
		if((msgQReceive(balMsgEnv, (char *) trame, sizeof(Trame), WAIT_FOREVER)) != ERROR)
        {
			printf("%s",trame->content);
        }
		else
		{
			
		}
		free(trame);
		taskDelay(60);
	}
}



TacheInfo tachesInfo[NB_TACHES] = {
  	{"Reception Ordre", 100, 0, 2048, tReceptOrdre},   //T_RECEPTORDRE
  	{"Envoi",           75, 0, 2048, tEnvoi},          //T_ENVOI
	{"Conditionnement", 95, 0, 2048, tConditionnement},//T_CONDITIONNEMENT
	{"Mise en Palette", 90, 0, 2048, tMiseEnPalette},  //T_MISEENPALETTE
	{"Stockage",        85, 0, 2048, tStockage},       //T_STOCKAGE
	{"Expedition",      80, 0, 2048, tExpedition},     //T_EXPEDITION
	{"Production",      75, 0, 2048, tProduction},     //T_PRODUCTION
	{"Journal",         75, 0, 2048, tJournal},        //T_JOURNAL	
  	{"Log",         	105, 0, 2048, tLog}            //T_LOG
};
pid_t pidMere;


MSG_Q_ID balPalette;
MSG_Q_ID balCarton;
MSG_Q_ID balOrdreCond;
MSG_Q_ID balOrdreExpe;
MSG_Q_ID balMessageLog;
MSG_Q_ID balJournal;
MSG_Q_ID balMsgRecv;
MSG_Q_ID balMsgEnv;


BalInfo balInfo[NB_BAL] = {
	{&balPalette,		MAXMSG_BALPALETTE,		sizeof(MsgPalette),			MSG_Q_FIFO},//BAL_PALETTE
	{&balCarton,		MAXMSG_BALCARTON,		sizeof(MsgCarton),			MSG_Q_FIFO},//BAL_CARTON
	{&balOrdreCond,		MAXMSG_BALORDRECOND,	sizeof(MsgOrdreCond),		MSG_Q_FIFO},//BAL_ORDRECOND
	{&balOrdreExpe,		MAXMSG_BALORDREEXPE,	sizeof(MsgOrdreExpe),		MSG_Q_FIFO},//BAL_ORDREEXP
	{&balMessageLog,	MAXMSG_BALMESSAGELOG,	TAILLEMSG_BALMESSAGELOG,	MSG_Q_PRIORITY},//BAL_MESSAGELOG
	{&balJournal,		MAXMSG_BALJOURNAL,		TAILLEMSG_BALMESSAGELOG,	MSG_Q_FIFO},//BAL_JOURNAL
	{&balMsgRecv,		MAXMSG_BALMSGRECV,		TAILLEMSG_BALMESSAGELOG,	MSG_Q_FIFO},//BAL_MSGRECV
	{&balMsgEnv,		MAXMSG_BALMSGENV,		TAILLEMSG_BALMESSAGELOG,	MSG_Q_FIFO}//BAL_MSGENV
};


pid_t carteTache[NB_TACHES];


Entrepot entrepot;


SEM_ID semCapteurDim;
int shmCapteurDim;


void lancerTache(int tacheNo)
{
	if (tacheNo == -1)
	{
		int i;
		for (i = 0 ; i < NB_TACHES ; i++)
		{
			lancerTache(i);
		}
	}
	else if (tacheNo < NB_TACHES)
	{
		carteTache[tacheNo]=taskSpawn(
			tachesInfo[tacheNo].name, 
			tachesInfo[tacheNo].priority, 
			tachesInfo[tacheNo].options, 
			tachesInfo[tacheNo].stackSize, 
			tachesInfo[tacheNo].entryPt,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
		DBG("Tache %c est cree\n", tachesInfo[tacheNo].name);
	}
}


void tuerTache(int tacheNo)
{
	if (tacheNo == -1)
	{
		int i;
		for(i = 0 ; i < NB_TACHES ; i++)
		{
			tuerTache(i);
		}
	}
	else if (tacheNo < NB_TACHES)
	{
		taskDelete(carteTache[tacheNo]);
		carteTache[tacheNo]=0;
	}
}


void creerBAL(int balNo)
{
	if (balNo == -1)
	{
		int i;
		for (i = 0 ; i < NB_BAL ; i++)
		{
			creerBAL(i);
		}
	}
	else if (balNo < NB_BAL)
	{
		*balInfo[balNo].id=msgQCreate(
			balInfo[balNo].maxMsg,
			balInfo[balNo].maxTaille, 
			balInfo[balNo].options);
	}
}


void fermerBAL(int balNo)
{
	if (balNo == -1)
	{
		int i;
		for (i = 0 ; i < NB_BAL ; i++)
		{
			fermerBAL(i);
		}
	}	
	else if (balNo < NB_BAL)
	{
		msgQDelete(*balInfo[balNo].id);
	}
}


void launch()
{
	taskSpawn("Mere", 100, 0, 2048, (FUNCPTR)tMere, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}


void tMere()
{
	
	pidMere = taskIdSelf();
	DBG("Initialisation\n");
	
	entrepot = NULL;
	semCapteurDim = semBCreate(SEM_Q_FIFO, SEM_EMPTY);
	shmCapteurDim = -1;
	srand(time(NULL));
	
	
	creerBAL(-1);
	
	
	lancerTache(-1);
	
	taskSuspend(0);

	
	semDelete(semCapteurDim);
	tuerTache(-1);
	fermerBAL(-1);
	DBG("Fin de tMere.");
}


